package com.ktgj.customer.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.Itinerary;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.ItineraryMapper;
import com.ktgj.customer.service.ILoginService;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.JourneyFlightOrderInfoVoApi;
import com.ktgj.hotel.api.RemoteHotelService;
import com.ktgj.hotel.api.domain.JourneyHotelVoApi;
import com.ktgj.service.api.RemoteService;
import com.ktgj.service.api.domain.JourneyOrderBoardingVoApi;
import com.ktgj.service.api.domain.JourneyOrderViphallVoApi;
import com.ktgj.train.api.RemoteTrainService;
import com.ktgj.train.api.domain.JourneyTrainOrderVoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.ItineraryMiddleMapper;
import com.ktgj.customer.domain.ItineraryMiddle;
import com.ktgj.customer.service.IItineraryMiddleService;

import javax.servlet.http.HttpServletRequest;

/**
 * 行程定制业务中间Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@Service
public class ItineraryMiddleServiceImpl implements IItineraryMiddleService 
{
    @Autowired
    private ItineraryMiddleMapper itineraryMiddleMapper;

    @Autowired
    private ItineraryMapper itineraryMapper;

    @Autowired
    private RemoteHotelService remoteHotelService;

    @Autowired
    private RemoteFlightService remoteFlightService;

    @Autowired
    private RemoteTrainService remoteTrainService;

    @Autowired
    private RemoteService remoteService;

    @Autowired
    private ILoginService loginService;

    /**
     * 查询行程定制业务中间
     * 
     * @param primaryKeyId 行程定制业务中间ID
     * @return 行程定制业务中间
     */
    @Override
    public ItineraryMiddle selectItineraryMiddleById(Long primaryKeyId)
    {
        return itineraryMiddleMapper.selectItineraryMiddleById(primaryKeyId);
    }

    /**
     * 查询行程定制业务中间列表
     * 
     * @param itineraryMiddle 行程定制业务中间
     * @return 行程定制业务中间
     */
    @Override
    public List<ItineraryMiddle> selectItineraryMiddleList(ItineraryMiddle itineraryMiddle)
    {
        return itineraryMiddleMapper.selectItineraryMiddleList(itineraryMiddle);
    }

    /**
     * 新增行程定制业务中间
     * 
     * @param itineraryMiddle 行程定制业务中间
     * @return 结果
     */
    @Override
    public int insertItineraryMiddle(ItineraryMiddle itineraryMiddle)
    {
        return itineraryMiddleMapper.insertItineraryMiddle(itineraryMiddle);
    }

    /**
     * 修改行程定制业务中间
     * 
     * @param itineraryMiddle 行程定制业务中间
     * @return 结果
     */
    @Override
    public int updateItineraryMiddle(ItineraryMiddle itineraryMiddle)
    {
        return itineraryMiddleMapper.updateItineraryMiddle(itineraryMiddle);
    }

    /**
     * 批量删除行程定制业务中间
     * 
     * @param primaryKeyIds 需要删除的行程定制业务中间ID
     * @return 结果
     */
    @Override
    public int deleteItineraryMiddleByIds(Long[] primaryKeyIds)
    {
        return itineraryMiddleMapper.deleteItineraryMiddleByIds(primaryKeyIds);
    }

    /**
     * 删除行程定制业务中间信息
     * 
     * @param primaryKeyId 行程定制业务中间ID
     * @return 结果
     */
    @Override
    public int deleteItineraryMiddleById(Long primaryKeyId)
    {
        return itineraryMiddleMapper.deleteItineraryMiddleById(primaryKeyId);
    }

    /**
     * 订单加入行程
     * */
    @Override
    public int addItineraryMiddle(ItineraryMiddleVo itineraryMiddleVo) throws Exception {
        int i = 0;
        if (itineraryMiddleVo.getItineraryCustomizeId() != null && itineraryMiddleVo.getItineraryCustomizeId() != 0){
            if (itineraryMiddleVo.getItineraryParameterVoList() != null && itineraryMiddleVo.getItineraryParameterVoList().size() > 0){
                for (ItineraryParameterVo itineraryParameterVo : itineraryMiddleVo.getItineraryParameterVoList()) {
                    if (itineraryParameterVo.getBusinessType() != null && !itineraryParameterVo.getBusinessType().equals("")){
                        if (itineraryParameterVo.getOrderId() != null && itineraryParameterVo.getOrderId().size() > 0){
                            for (Long aLong : itineraryParameterVo.getOrderId()) {
                                ItineraryMiddle itineraryMiddle = new ItineraryMiddle();
                                itineraryMiddle.setItineraryCustomizeId(itineraryMiddleVo.getItineraryCustomizeId());
                                itineraryMiddle.setBusinessType(itineraryParameterVo.getBusinessType());
                                itineraryMiddle.setBusinessLineId(aLong);
                                i = itineraryMiddleMapper.insertItineraryMiddle(itineraryMiddle);
                            }
                        }
                        else {
                            throw new Exception("订单id不能为空");
                        }
                    }
                    else {
                        throw new Exception("业务线类型不能为空");
                    }
                }
            }
            else {
                throw new Exception("关联订单信息不能为空");
            }
        }
        else {
            throw new Exception("行程指定id");
        }
        return i;
    }

    /**
     * 查看行程
     * */
    @Override
    public ItineraryOrderVo selecItineraryMiddle(Long itineraryCustomizeId) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Itinerary itinerary = itineraryMapper.journeyItinerary(itineraryCustomizeId);
        if (itinerary == null){
            throw new Exception("该行程不存在");
        }
        List<ItineraryMiddle> itineraryMiddles = itineraryMiddleMapper.itineraryMiddleList(itineraryCustomizeId);
        if (itineraryMiddles != null && itineraryMiddles.size() > 0){
            ItineraryOrderVo itineraryOrderVo = new ItineraryOrderVo();
            itineraryOrderVo.setEnd(itinerary.getDestination());
            itineraryOrderVo.setDepartureTime(format.format(itinerary.getDepartureTime()));
            itineraryOrderVo.setJourney(itinerary.getDeparturePoint() + "前往" + itinerary.getDestination());

            List<OrderVo> orderVoList = new ArrayList<>();

            String stateHotel = null;//酒店状态
            String stateFlight = null;//机票状态
            String stateTrain = null;//火车票状态
            String stateService = null;//贵宾厅状态
            String stateVIPAisle = null;//vip通道状态
            String stateCheckIn = null;//值机状态

            for (ItineraryMiddle itineraryMiddle : itineraryMiddles) {
                OrderVo orderVo = new OrderVo();
                orderVo.setId(itineraryMiddle.getPrimaryKeyId());
                if (itineraryMiddle.getBusinessType() != null){
                    //酒店
                    if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.HOTEL.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderId", itineraryMiddle.getBusinessLineId());
                        JourneyHotelVoApi journeyHotelVoApi = remoteHotelService.journeyHotelVo(map);
                        orderVo.setOrderId(journeyHotelVoApi.getOrderId());
                        orderVo.setBusinessType(journeyHotelVoApi.getBusinessType());
                        orderVo.setActualPriceTotal(journeyHotelVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyHotelVoApi.getOrderStatusID());
                        orderVo.setHotelName(journeyHotelVoApi.getHotelName());
                        orderVo.setNumberOfRooms(journeyHotelVoApi.getNumberOfRooms());
                        orderVo.setArriveTime(journeyHotelVoApi.getArriveTime());
                        orderVo.setCheckOutTime(journeyHotelVoApi.getCheckOutTime());
                        orderVoList.add(orderVo);
                        stateHotel = journeyHotelVoApi.getOrderStatusID();
                    }
                    //机票
                    else if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.FLIGHT.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderId", itineraryMiddle.getBusinessLineId());
                        JourneyFlightOrderInfoVoApi journeyFlightOrderInfoVoApi = remoteFlightService.journeyFlightOrderInfo(map);
                        orderVo.setOrderId(journeyFlightOrderInfoVoApi.getOrderId());
                        orderVo.setAirlineLogo(journeyFlightOrderInfoVoApi.getAirlineLogo());
                        orderVo.setBusinessType(journeyFlightOrderInfoVoApi.getBusinessType());
                        orderVo.setCreateTime(journeyFlightOrderInfoVoApi.getCreateTime());
                        orderVo.setElapsedTime(journeyFlightOrderInfoVoApi.getElapsedTime());
                        orderVo.setActualPriceTotal(journeyFlightOrderInfoVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyFlightOrderInfoVoApi.getOrderStatusID());
                        orderVo.setAirliner(journeyFlightOrderInfoVoApi.getAirliner());
                        orderVo.setDepartureTerminal(journeyFlightOrderInfoVoApi.getDepartureTerminal());
                        orderVo.setArrivalTerminal(journeyFlightOrderInfoVoApi.getArrivalTerminal());
                        orderVo.setFlight(journeyFlightOrderInfoVoApi.getFlight());
                        orderVo.setDeparture(journeyFlightOrderInfoVoApi.getDeparture());
                        orderVo.setArrival(journeyFlightOrderInfoVoApi.getArrival());
                        orderVo.setDepartureTime(journeyFlightOrderInfoVoApi.getDepartureTime());
                        orderVo.setArrivalTime(journeyFlightOrderInfoVoApi.getArrivalTime());
                        orderVoList.add(orderVo);
                        stateFlight = journeyFlightOrderInfoVoApi.getOrderStatusID();
                    }
                    //火车票
                    else if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.TRAIN.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderid", itineraryMiddle.getBusinessLineId());
                        JourneyTrainOrderVoApi journeyTrainOrderVoApi = remoteTrainService.journeyTrainOrder(map);
                        orderVo.setOrderId(journeyTrainOrderVoApi.getOrderId());
                        orderVo.setBusinessType(journeyTrainOrderVoApi.getBusinessType());
                        orderVo.setActualPriceTotal(journeyTrainOrderVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyTrainOrderVoApi.getOrderStatusID());
                        orderVo.setTrainnumber(journeyTrainOrderVoApi.getTrainnumber());
                        orderVo.setDeparturestationname(journeyTrainOrderVoApi.getDeparturestationname());
                        orderVo.setArrivalstationname(journeyTrainOrderVoApi.getArrivalstationname());
                        orderVo.setDeparturetime(journeyTrainOrderVoApi.getDeparturetime());
                        orderVo.setArrivaltime(journeyTrainOrderVoApi.getArrivaltime());
                        orderVo.setTraveldate(journeyTrainOrderVoApi.getTraveldate());
                        orderVoList.add(orderVo);
                        stateTrain = journeyTrainOrderVoApi.getOrderStatusID();
                    }
                    //贵宾厅
                    else if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.SERVICE.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderviphallId", itineraryMiddle.getBusinessLineId());
                        JourneyOrderViphallVoApi journeyOrderViphallVoApi = remoteService.journeyOrderViphallVo(map);
                        orderVo.setOrderId(journeyOrderViphallVoApi.getOrderId());
                        orderVo.setBusinessType(journeyOrderViphallVoApi.getBusinessType());
                        orderVo.setActualPriceTotal(journeyOrderViphallVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyOrderViphallVoApi.getOrderStatusID());
                        orderVo.setContactPhone(journeyOrderViphallVoApi.getContactPhone());
                        orderVo.setPeopleNumber(journeyOrderViphallVoApi.getPeopleNumber());
                        orderVo.setViphallName(journeyOrderViphallVoApi.getViphallName());
                        orderVo.setAddress(journeyOrderViphallVoApi.getAddress());
                        orderVo.setBusinessBeginhour(journeyOrderViphallVoApi.getBusinessBeginhour());
                        orderVo.setBusinessEndhour(journeyOrderViphallVoApi.getBusinessEndhour());
                        orderVoList.add(orderVo);
                        stateService = journeyOrderViphallVoApi.getOrderStatusID();
                    }
                    //vip通道
                    else if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.VIPAisle.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("ordervipchannelId", itineraryMiddle.getBusinessLineId());
                        JourneyOrderViphallVoApi journeyOrderViphallVoApi = remoteService.journeyOrderVipchannelVo(map);
                        orderVo.setOrderId(journeyOrderViphallVoApi.getOrderId());
                        orderVo.setBusinessType(journeyOrderViphallVoApi.getBusinessType());
                        orderVo.setActualPriceTotal(journeyOrderViphallVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyOrderViphallVoApi.getOrderStatusID());
                        orderVo.setContactPhone(journeyOrderViphallVoApi.getContactPhone());
                        orderVo.setPeopleNumber(journeyOrderViphallVoApi.getPeopleNumber());
                        orderVo.setViphallName(journeyOrderViphallVoApi.getViphallName());
                        orderVo.setAddress(journeyOrderViphallVoApi.getAddress());
                        orderVoList.add(orderVo);
                        stateVIPAisle = journeyOrderViphallVoApi.getOrderStatusID();
                    }
                    //值机
                    else if (itineraryMiddle.getBusinessType().equals(EmHelper.businessType.CheckIn.getValue())){
                        Map<String, Object> map = new HashMap<>();
                        map.put("orderboardingId", itineraryMiddle.getBusinessLineId());
                        JourneyOrderBoardingVoApi journeyOrderBoardingVoApi = remoteService.selectOrderBoardingDetailed(map);
                        orderVo.setOrderId(journeyOrderBoardingVoApi.getOrderId());
                        orderVo.setBusinessType(journeyOrderBoardingVoApi.getBusinessType());
                        orderVo.setActualPriceTotal(journeyOrderBoardingVoApi.getActualPriceTotal());
                        orderVo.setOrderStatusID(journeyOrderBoardingVoApi.getOrderStatusID());
                        orderVo.setFlightNumber(journeyOrderBoardingVoApi.getFlightNumber());
                        orderVo.setDepartureCity(journeyOrderBoardingVoApi.getDepartureCity());
                        orderVo.setArrivalCity(journeyOrderBoardingVoApi.getArrivalCity());
                        orderVoList.add(orderVo);
                        stateCheckIn = journeyOrderBoardingVoApi.getOrderStatusID();
                    }
                    else {
                        throw new Exception("好好看看添加订单时的业务线类型，" + itineraryMiddle.getBusinessType() + "这是哪个业务线？");
                    }
                }
                else {
                    throw new Exception("行程中的订单出现错误，请联系工作人员进行核实");
                }
            }

            String ordersState;
            if (stateHotel != null && !stateHotel.equals("") &&
                    stateFlight != null && !stateFlight.equals("") &&
                    stateTrain != null && !stateTrain.equals("") &&
                    stateService != null && !stateService.equals("") &&
                    stateVIPAisle != null && !stateVIPAisle.equals("") &&
                    stateCheckIn != null && !stateCheckIn.equals("")){
                if (stateHotel.equals("未支付") || stateFlight.equals("未支付") ||
                        stateTrain.equals("未支付") || stateService.equals("未支付") ||
                        stateVIPAisle.equals("未支付") || stateCheckIn.equals("未支付")){
                    ordersState = EmHelper.OrderStatus.Unpaid.getValue();
                }
                else {
                    ordersState = EmHelper.OrderStatus.Paid.getValue();
                }
                itineraryMapper.updateStatic(ordersState, itinerary.getItineraryCustomizeId());
            }

            itineraryOrderVo.setOrderVoList(orderVoList);
            return itineraryOrderVo;
        }
        else {
            throw new Exception("您还没有规划行程哦");
        }
    }

    /**
     * 移除行程中的订单
     * */
    @Override
    public int delItineraryMiddle(Long primaryKeyId) {
        int i = itineraryMiddleMapper.delItineraryMiddle(primaryKeyId);
        return i;
    }

    /**
     * 行程列表
     * */
    @Override
    public TmpItineraryVo journeyList(HttpServletRequest request) throws Exception {
        TmpItineraryVo tmpItineraryVo = new TmpItineraryVo();
        String token = request.getHeader("token");
        Customer customer = loginService.getLoginCustomer(request);
        if (customer == null){
            throw new Exception("登录已过期");
        }
        List<Itinerary> itinerarys = itineraryMapper.queryHistoryIyinerary(customer.getCustomerId());
        List<Itinerary> itineraryMiddles = itineraryMapper.journeyList(customer.getCustomerId());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date createTime = loginService.getLoginCustomerByToken(token).getCreateTime();
        if (createTime != null) {
            String format = dateFormat.format(createTime);
            try {
                Date date = new Date();
                Date create = dateFormat.parse(format);
                long time = date.getTime();
                long createDateTime = create.getTime();
                long joinDays = (time - createDateTime) / 60 / 60 / 24 / 1000;
                tmpItineraryVo.setJoinDays(joinDays);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        tmpItineraryVo.setTotalMileage("35167km");
        tmpItineraryVo.setServiceNumber(Long.valueOf(itinerarys.size()));
        tmpItineraryVo.setItineraryList(itineraryMiddles);
        return tmpItineraryVo;
    }
}
